﻿using SD.Infrastructure.Repository.EntityFrameworkCore;
using SlamDunk.MES.Domain.Entities.AcquisitionContext;
using SlamDunk.MES.Domain.IRepositories.Interfaces.AcquisitionContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SlamDunk.MES.Repository.Implements.AcquisitionContext
{
    /// <summary>
    /// 采集会话仓储实现
    /// </summary>
    public class AcquisitionSessionRepository : EFAggRootRepositoryProvider<AcquisitionSession>, IAcquisitionSessionRepository
    {
        #region # 分页获取采集会话列表 —— ICollection<AcquisitionSession> FindByPage(Guid? acquisitionRuleId...
        /// <summary>
        /// 分页获取采集会话列表
        /// </summary>
        /// <param name="acquisitionRuleId">采集规则Id</param>
        /// <param name="processId">工艺Id</param>
        /// <param name="processSegmentId">工艺段Id</param>
        /// <param name="executionPlanId">作业计划Id</param>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="active">是否活动</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>采集会话列表</returns>
        public ICollection<AcquisitionSession> FindByPage(Guid? acquisitionRuleId, Guid? processId, Guid? processSegmentId, Guid? executionPlanId, Guid? workOrderId, bool? active, DateTime? startTime, DateTime? endTime, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            Expression<Func<AcquisitionSession, bool>> condition =
                x =>
                    (acquisitionRuleId == null || x.AcquisitionRuleId == acquisitionRuleId) &&
                    (processId == null || x.ProcessId == processId) &&
                    (processSegmentId == null || x.ProcessSegmentId == processSegmentId) &&
                    (executionPlanId == null || x.ExecutionPlanId == executionPlanId) &&
                    (workOrderId == null || x.WorkOrderId == workOrderId) &&
                    (active == null || x.Active == active) &&
                    (startTime == null || x.AddedTime >= startTime) &&
                    (endTime == null || x.AddedTime <= endTime);

            IQueryable<AcquisitionSession> acquisitionSessions = base.FindByPage(condition, pageIndex, pageSize, out rowCount, out pageCount);

            return acquisitionSessions.ToList();
        }
        #endregion
    }
}
